package com.aimee.mvc.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.aimee.mvc.R;
import com.aimee.mvc.bean.Account;
import com.aimee.mvc.callback.ResultCallBack;
import com.aimee.mvc.model.MVCModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MVCActivity extends AppCompatActivity {

    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.btn_search)
    Button btnSearch;
    @BindView(R.id.tv_result)
    TextView tvResult;

    private MVCModel mvcModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvc);
        ButterKnife.bind(this);
        mvcModel = new MVCModel();
    }

    @OnClick(R.id.btn_search)
    public void onViewClicked() {
        /*
         * 把数据传给Model，让Model进行数据处理，然后Model处理完后，通过CallBack将结果返回，然后进行界面更新
         */
        mvcModel.getAccountData(getUserInput(), new ResultCallBack() {
            @Override
            public void onSuccess(Account account) {
                showSuccessPage(account);
            }

            @Override
            public void onFail() {
                showFailPage();
            }
        });
    }

    private String getUserInput() {
        return etAccount.getText().toString();
    }

    private void showSuccessPage(Account account) {
        tvResult.setText(String.format(getResources().getString(R.string.str_result_name_and_level),
                account.getName(), account.getLevel()));
    }

    private void showFailPage() {
        tvResult.setText(R.string.str_get_data_fail);
    }
}
